﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryMIS_SE01
{
    public partial class ConnectionDB : Form
    {
        public static string connStr;//数据库连接对象（字符串）
        public string db = "", username = "", pwd = "", hostname = "";

        SqlConnection conn;//数据库连接对象Connection

        private void button3_Click(object sender, EventArgs e)
        {
            this.textBoxServerName.Clear();
            this.textBoxDBName.Clear();
            this.textBoxUserName.Clear();
            this.textBoxPWD.Clear();
        }

        public ConnectionDB()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            hostname = textBoxServerName.Text.Trim();//将服务器名称输入去除空格取值
            db = textBoxDBName.Text.Trim();
            username = textBoxUserName.Text.Trim();
            pwd = textBoxPWD.Text.Trim();

            if (String.IsNullOrEmpty(hostname))
            {
                MessageBox.Show("服务器名称是必填字段。", "系统提示：");
                this.textBoxServerName.Focus();
                return;
            }
            if (String.IsNullOrEmpty(db))
            {
                MessageBox.Show("数据库名称是必填字段。", "系统提示：");
                this.textBoxDBName.Focus();
                return;
            }
            if (String.IsNullOrEmpty(username))
            {
                MessageBox.Show("用户名是必填字段。", "系统提示：");
                this.textBoxUserName.Focus();
                return;
            }
            if (String.IsNullOrEmpty(pwd))
            {
                MessageBox.Show("密码是必填字段。", "系统提示：");
                this.textBoxPWD.Focus();
                return;
            }
            if(!String.IsNullOrEmpty(hostname) && 
                !String.IsNullOrEmpty(db) &&
                !String.IsNullOrEmpty(username) &&
                !String.IsNullOrEmpty(pwd))
            {
                try
                {
                    //组数据库连接字符串
                    connStr = "Server=" + hostname +
                        ";User Id=" + username +
                        ";Pwd=" + pwd +
                        ";DataBase=" + db;
                    conn = new SqlConnection(connStr);//用63行DB连接字符串实例化数据库连接对象
                    conn.Open();
                    if (conn.State == ConnectionState.Open)
                        textBoxResult.Text = "数据库已成功连接！";
                }
                catch (Exception ex)
                {
                    textBoxResult.Text = ex.Message;

                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                conn.Close();
                if (conn.State == ConnectionState.Closed)
                    this.textBoxResult.Text = "数据库连接关闭成功。";
            }
            catch (Exception ex)
            {
                this.textBoxResult.Text = ex.Message;
            }
            
        }


    }
}
